var QQMapWX = require('../libs/qqmap-wx-jssdk.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buthidden: false,
    currentLat: "",
    currentLon: "",
    address: "",
    detailAddress: "",
    location: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getLocationData();
  },

  //手机号前端校验
  onPhoneEvent: function (e) {
    var that = this;
    if (e.detail.value.length == 1) {
      that.setData({ telephone: e.detail.value.replace(/[^1-9]/g, '') });
    } else {
      that.setData({ telephone: e.detail.value.replace(/\D/g, '') });
    }
  },

  //获取当前经纬度
  getLocationData: function () {
  
    var that = this;
    wx.getLocation({
      type: 'gcj02',
     // type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var locationDic = { 'latitude': latitude, 'longitude': longitude };
        wx.setStorage({
          key: 'map_Location',
          data: locationDic,
        });
        that.setData({
          currentLat: latitude,
          currentLon: longitude,
        });

        var qqmapsdk = new QQMapWX({
          key: 'O5GBZ-HWGC3-UF63Q-YHQJJ-4BPY3-MMFUU'
        });
        // 调用接口
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: that.data.currentLat,
            longitude: that.data.currentLon
          },
          success: function (res) {
            //console.log(res);
          },
          fail: function (res) {
            //console.log(res);
          },
          complete: function (res) {
            var address = res.result.address_component.province + ' ' + res.result.address_component.city + ' ' + res.result.address_component.district;
            var detailAddress = res.result.address_component.street;
            var location = res.result.location.lat + ',' + res.result.location.lng;
            that.setData({
              address: address,
              detailAddress: detailAddress,
              location: location
            });
          }
        });
      }
    })
  },

  // 选择省市区
  chooseAddress: function (e) {
    wx.navigateTo({
      url: '../mine/mapLocation/searchMapLocation/searchMapLocation',
    })
  },

  //提交按钮
  bindFormSubmit: function (e) {
    
    var that = this;
    var name = e.detail.value.name;
    var telephone = e.detail.value.telephone;
    var address = e.detail.value.address;
    var detailAddress = e.detail.value.detailAddress;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var province = (that.data.address).split(" ")[0];
    var city = (that.data.address).split(" ")[1];
    var area = (that.data.address).split(" ")[2];
    var lat = (that.data.location).split(",")[0];
    var lng = (that.data.location).split(",")[1];
    if (!name || !telephone ||!address || !detailAddress) {
      wx.showToast({
        title: '各项信息必填',
        icon: "none"
      })
      return false;
    } else if (telephone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (!myreg.test(telephone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (city != "苏州市") {
      wx.showModal({
        title: '该地区暂不支持',
        confirmColor: '#56BDE0',
        showCancel: false,
      })
      return false;
    }else {
      that.setData({ buthidden: true });
      
      wx.request({
        method: "POST",
        url: 'https://www.zaishengwang.com/wx/openRegisterUser.do',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded;',
        },
        data: {
          openId: app.globalData.openId,
          contacts:name,
          telephone:telephone,
          province: province,
          city: city,
          area: area,
          address: detailAddress,
          lat: lat,
          lng: lng
         // detailAddress: detailAddress,
          //location: that.data.location
        },
        success: function (res) {
          // wx.setStorage({
          //   key: 'userId',
          //   data: res.data.userVo.bId,
          // });
           
          if (res.data.msg=="2"){
            wx.showModal({
              title: '该手机号已注册',
              confirmColor: '#56BDE0',
              showCancel: false,
            })
            that.setData({ buthidden: false });
          }else {
            var  c = 1;
            var userId = res.data.userVo.bId
            app.globalData.userId = userId;
            wx.setStorageSync('userId', userId);

            wx.showToast({
              title: '注册成功',
              icon: 'success',
              duration: 1000
            });
            var intervalId = setInterval(function () {
              c = c - 1;
              if (c == 0) {
                clearInterval(intervalId);
                wx.reLaunch({
                  url: '../mine/mine',
                })
              }
            }, 1000)
            
          }

        },
        fail: function (error) {
          that.setData({ buthidden: false });
        }

      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})